#include "stdafx.h"
#include "SceneMsgHandler.h"
#include "Scene/Scene.h"
#include "Scene/SceneLua.h"
#include "Service/Service.h"
#include "Message/MsgScene.h"
#include "Message/DBMessage.h"
#include "Player/Progress.h"
#include "Player/ProgressLua.h"
#include "Scene/SceneManagerLua.h"
#include "Entity/EntityVisitorCombat.h"


SceneMsgHandler::SceneMsgHandler()
:m_wpScnMgr(NULL), m_wpEttMgrWorld(NULL), m_wpCurEtt(NULL)
{
	
}

SceneMsgHandler::~SceneMsgHandler()
{

}

bool SceneMsgHandler::initialize(IMsgHandlerInitData* data)
{
	MsgHandlerInitDataLgc* d = dynamic_cast<MsgHandlerInitDataLgc*>(data);
	if (d)
	{
		m_wpScnMgr = d->wpScnMgr;
		m_wpEttMgrWorld = d->wpEttMgrW;
		m_wpPlayerDataMgr = d->wpPlayerDataMgr;
		return __super::initialize(data);
	}
	else
		return false;
}



bool SceneMsgHandler::handleMsg(IMsg* pMsg)
{
	int nMsgCate = pMsg->getMsgCate();
	
	m_wpCurEtt = m_wpEttMgrWorld->getEntity(pMsg->getSID());
	m_wpCurPlayer = m_wpPlayerDataMgr->get(pMsg->getSID());

	if (m_wpCurEtt==NULL)
	{
		LOG_ERROR("Failed to handle scene message , invalid session id: "<<pMsg->getSID());
		return false;
	}

	if(nMsgCate < Msg::MC_Cli_Max)
	{
		onRcvCliMsg(pMsg);
	}

	return true;
}

void SceneMsgHandler::onRcvCliMsg(IMsg* pMsg)
{
	switch(pMsg->getMsgID())
	{
	case MsgEnterDistrictRequest::Id:
		{
			MsgEnterDistrictRequest* msg = dynamic_cast<MsgEnterDistrictRequest*>(pMsg);
			if (msg)
			{
				MsgEnterDistrictResponse* response = new MsgEnterDistrictResponse;
				Progress* pw= m_wpCurPlayer!=NULL? m_wpCurPlayer->getProgressData() : NULL;
				if (pw!=NULL)
				{
					Lua::instance()->push(int(msg->cid));
					Lua::instance()->push(int(msg->did));
					Lua::instance()->push(int(m_wpCurEtt->getLevel()));
					
					bool bSuccess = false;
					if (Lua::instance()->call("Scene", "onCheckDistrict", 3, 1))
					{
						Lua::instance()->pop(bSuccess);
					};

					if (bSuccess)
					{
						ProgressDistrict* pd = pw->get(msg->cid, msg->did);
						if (pd)
						{
							SceneProgress sp;
							for (ProgressDistrict::iterator it = pd->begin();
								it!=pd->end();
								it++)
							{
								sp.sid	= it->first;
								sp.data	= it->second;
								response->sps.push_back(sp);
							}
						}

						response->errcode = ERR_SUCCESS;
					}
					else
					{
						response->errcode = ERR_INVALID_PARAMS;
					}
					
				}
				else
				{
					LOG_ERROR("Progress data not loaded yet for session: "<<pMsg->getSID());
					response->errcode = ERR_DATA_NOT_READY;
				}

				response->setSID(msg->getSID());
				getSvcHost()->postMsg(response);
			}
			break;
		}
	case MsgEnterSceneRequest::Id:
		{
			MsgEnterSceneRequest* msg = dynamic_cast<MsgEnterSceneRequest*>(pMsg);
			if (msg)
			{
				MsgEnterSceneResponse* response = new MsgEnterSceneResponse;
				Progress* pw= m_wpCurPlayer!=NULL? m_wpCurPlayer->getProgressData() : NULL;
				if (pw)
				{
					Lua::instance()->push(int(msg->cid));
					Lua::instance()->push(int(msg->did));
					Lua::instance()->push(int(msg->sid));

					SceneManagerLua::push(Lua::instance()->state(), m_wpScnMgr);
					ProgressLua::push(Lua::instance()->state(), pw);

					if (Lua::instance()->call("Scene", "onCreate", 5, 1))
					{
						int nInstanceID = 0;
						if (Lua::instance()->pop(nInstanceID) && nInstanceID!=0)
						{
							response->dsid = nInstanceID;
							response->errcode = ERR_SUCCESS;
						}
						else
						{
							LOG_ERROR("Failed to create scene for session: "<<pMsg->getSID());
							response->errcode = ERR_FAILED;
						}
					}
					else
					{
						response->errcode = ERR_FAILED;
					}
				}
				else
				{
					LOG_ERROR(" No progress loaded for session: "<<pMsg->getSID());
					response->errcode = ERR_DATA_NOT_READY;
				}

				getSvcHost()->postMsg(response);
			}
			break;
		}
	case MsgCombatStartRequest::Id:
		{
			MsgCombatStartRequest* msg = dynamic_cast<MsgCombatStartRequest*>(pMsg);
			if (msg)
			{
				MsgCombatStartResponse* response = new MsgCombatStartResponse;

				Progress* pw= m_wpCurPlayer!=NULL? m_wpCurPlayer->getProgressData() : NULL;
				if (pw!=NULL)
				{
					IScene* pScene = m_wpScnMgr->getScene(msg->dsid);
					if (pScene)
					{
						SceneLua::push(Lua::instance()->state(), pScene);
						ProgressLua::push(Lua::instance()->state(), pw);
						Lua::instance()->push((int)msg->wid);

						bool bSucess = false;
						if (Lua::instance()->call("Scene", "onStartCombat", 3, 1))
						{
							Lua::instance()->pop(bSucess);
						}

						if (bSucess)
						{
							EntityVisitorCombatMonster visitor(*response);
							pScene->forEach(visitor);
						}
						response->errcode = ERR_SUCCESS;
					}
					else
					{
						LOG_ERROR("Failed to start combat, invalid scene instance id: "<<msg->dsid);
						response->errcode = ERR_INVALID_PARAMS;
					}
				}
				else
				{
					LOG_ERROR("Failed to start combat, progress data not ready!");
					response->errcode = ERR_DATA_NOT_READY;
				}
				getSvcHost()->postMsg(response);
			}
			break;
		}
	case MsgCombatEndRequest::Id:
		{
			MsgCombatEndRequest* msg = dynamic_cast<MsgCombatEndRequest*>(pMsg);
			if (msg)
			{
			}
			break;
		}
	}
}


